package com.burgeon.backend.SHOPBACKEND.service.Impl;

import com.burgeon.backend.SHOPBACKEND.entity.Brand;
import com.burgeon.backend.SHOPBACKEND.entity.Classify;
import com.burgeon.backend.SHOPBACKEND.mappers.GoodsClassifyItemMapper;
import com.burgeon.backend.SHOPBACKEND.mappers.GoodsBackendClassifyMapper;
import com.burgeon.backend.SHOPBACKEND.service.GoodsClassifyService;
import com.burgeon.backend.SHOPBACKEND.service.ex.ErrorDataException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class GoodsClassifyServiceImpl implements GoodsClassifyService {
    @Autowired
    private GoodsBackendClassifyMapper goodsClassifyMapper;
    @Autowired
    private GoodsClassifyItemMapper goodsClassifyItemMapper;


    @Override
    public void upBrandTop(String id,String account) throws ErrorDataException {
        Integer brandMinNub = goodsClassifyMapper.getBrandMinNub(account);
        Integer u2 = goodsClassifyMapper.upBrandNub(brandMinNub-1, id);
        if (u2<1){
            throw new ErrorDataException("出现错误");
        }

    }

    @Override
    @Transactional
    public void upBrandNub(String id, Integer nub, String account) throws ErrorDataException {

        if (id==null&&account==null){
            throw new ErrorDataException("出现错误");
        }
        int Idnub = goodsClassifyMapper.getBrandNubById(id);
        int bid = goodsClassifyMapper.getBrandNubByNubAndToken(account, Idnub + nub);
        Integer u1 = goodsClassifyMapper.upBrandNub(Idnub + nub, id);
        if (u1<1){
            throw new ErrorDataException("出现错误");
        }
        Integer u2 = goodsClassifyMapper.upBrandNub(Idnub, String.valueOf(bid));
        if (u2<1){
            throw new ErrorDataException("出现错误");
        }

    }

    @Override
    public void upBrandShow(String id,String isShow) {

        goodsClassifyMapper.upBrandShow(id,isShow);
    }

    @Override
    public List<Brand> getBrand(String account) {
        List<Brand> data = goodsClassifyMapper.showBrandData(account);
        for (Brand brand : data){
            brand.setCount(goodsClassifyMapper.getBrandGoodsCount(brand.getId()));
        }
        return data;
    }

    /**
     * 修改是否展示
     * @param isShow
     * @param id
     */

    @Override
    public void updateIsShowById(String isShow, String id) throws ErrorDataException {
        try {
            goodsClassifyMapper.updateIsShowById(isShow, id);
        }catch (Exception e){
            throw new ErrorDataException("修改展示出错");
        }
    }

    @Override
    public List<Classify> showClassify(String account) {
        return goodsClassifyMapper.getClassify(account);
    }

    //添加商品类型
    @Override
    public void insertClassify(Classify classify) throws ErrorDataException{
        try {
            goodsClassifyMapper.insertClassify(classify);
        } catch (Exception e) {
            throw new ErrorDataException("添加商品类型错误");
        }
    }

    //根据ID删除商品类型
    @Override
    public void deleteClassify(Integer id) throws ErrorDataException{
        System.err.println(id);
        try {
            goodsClassifyMapper.deleteClassify(id);
            goodsClassifyItemMapper.deClassifyItemByCid(String.valueOf(id));//根据大分类的id删除小分类
        } catch (Exception e) {
            e.printStackTrace();
            throw new ErrorDataException("删除商品类型错误");
        }
    }

    //根据ID修改商品类型
    @Override
    public void updateClassifyById(Classify classify) throws ErrorDataException{
        try {
            goodsClassifyMapper.updateClassifyById(classify);
        } catch (Exception e) {
            throw new ErrorDataException("修改商品类型错误");
        }
    }



}